//
//  DetailedDocumentViewController.m
//  FMS
//
//  Created by Steven on 11/3/13.
//  Copyright (c) 2013 QGSVN. All rights reserved.
//

#import "DetailedDocumentViewController.h"
#import "DocumentLinkViewController.h"
#import "DocumentViewController.h"
#import "ListMaintenanceAttachmentViewController.h"

@interface DetailedDocumentViewController ()

@end

@implementation DetailedDocumentViewController
@synthesize documentDict;
@synthesize documentImage;
@synthesize parent;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil itemType:(int)_itemType parent:(id)_parent
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
        itemType = _itemType;
        dictLanguage = [ApplicationDelegate.arrayLanguage objectAtIndex:0];
        parent = _parent;
    }
    return self;
}

- (IBAction)backPreviousView:(id)sender {
    [self dismissModalViewControllerAnimated:YES];
}

- (IBAction)handleLinkDocumentEvent:(id)sender {
    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    
    NSMutableDictionary *buildingPermissionDict;
    if (itemType == Building_Type)
        buildingPermissionDict = [userDefaults objectForKey:BUILDING_PERMISSION];
    else
        buildingPermissionDict = [userDefaults objectForKey:FACILITY_PERMISSION];
    
    //Get permission about this tab
    //if has Edit permission is also has Add permission
    NSMutableDictionary *permissionDict = [buildingPermissionDict objectForKey:@"documentAttachment"];
    BOOL isEditable = [[permissionDict objectForKey:@"update"] boolValue];
    if (!isEditable) {
        UIAlertView *dialog = [[UIAlertView alloc] initWithTitle:[dictLanguage objectForKey:@"general.warning"] message:[dictLanguage objectForKey:@"general.permission.no-right-access"] delegate:nil cancelButtonTitle:[dictLanguage objectForKey:@"general.ok"] otherButtonTitles:nil];
        [dialog show];
        return;
    }
    
    DocumentLinkViewController *controller = [[DocumentLinkViewController alloc] initWithNibName:@"DocumentLinkViewController" bundle:nil parent:self];
    controller.currentDocumentDict = documentDict;
    controller.documentImage = documentImage;
    [self.navigationController pushViewController:controller animated:YES];
}

- (void) reloadGrandView {
    [(DocumentViewController*)parent listDocumentsByItem];
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.navigationController.navigationBar.hidden = YES;
    
    NSData *documentData = UIImageJPEGRepresentation(documentImage, 1.0);
    [_mainWebView loadData:documentData MIMEType:@"image/gif" textEncodingName:nil baseURL:nil];
    _mainWebView.scalesPageToFit = YES;
    
    if ([parent isKindOfClass:[ListMaintenanceAttachmentViewController class]]) {
        self.btnEdit.hidden = YES;
    }
}

@end
